//
//  Work.swift
//  MyCookbook
//
//  Created by liaokylin on 2024/10/21.
//

import SwiftUI

struct Work: View {
    var body: some View {
        VStack(alignment:.leading){
            HStack{
                Image("5").resizable().frame(width: 35,height: 35).clipShape(Circle())
                Text("蓝莓妈妈")
                
                Spacer()
                Button {
                    
                } label: {
                    Text("关注")
                        .font(.caption)
                        .padding(EdgeInsets(top: 10, leading: 15, bottom: 10, trailing: 15))
                        .background(.pink.opacity(0.1))
                        .foregroundColor(.pink)
                }
                .cornerRadius(20)
                Image(systemName: "ellipsis")
            }
            NavigationLink {
                
            } label: {
                Text("#晚餐.2024日10月8日").foregroundColor(.orange)
            }
            Text("孩子们想吃的春卷安排上～")
            HStack{
                Image("1").resizable()
                    .frame(width: 100,height: 100)
                Image("1").resizable()
                    .frame(width: 100,height: 100)
                Image("1").resizable()
                    .frame(width: 100,height: 100)
            }.cornerRadius(10)
            HStack{
                Image(systemName:"hand.thumbsup")
                Text("8")
                
                Image(systemName: "bubble")
                Text("评论")
            }
        }.padding(.vertical,10)
    }
}

struct FlowLayout: View {
    let items: [String]
    let spacing: CGFloat = 10

    var body: some View {
     //   var totalWidth: CGFloat = 0
        return VStack(alignment: .leading, spacing: spacing) {
            ForEach(self.computeRows(items: items, totalWidth: UIScreen.main.bounds.width),id:\.self) { rowItems in
                HStack(spacing: spacing) {
                    ForEach(rowItems, id: \.self) { item in
                        Text(item)
                            .padding()
                            .background(Color.blue)
                            .cornerRadius(8)
                    }
                }
            }
        }
    }

    // 根据屏幕宽度计算行中的元素
    private func computeRows(items: [String], totalWidth: CGFloat) -> [[String]] {
        var rows: [[String]] = [[]]
        var currentWidth: CGFloat = 0

        for item in items {
            let itemWidth = (item as NSString).size(withAttributes: [.font: UIFont.systemFont(ofSize: 17)]).width + 40  // 假设每个元素的宽度
            if currentWidth + itemWidth + spacing > totalWidth {
                rows.append([item])
                currentWidth = itemWidth
            } else {
                rows[rows.count - 1].append(item)
                currentWidth += itemWidth + spacing
            }
        }
        return rows
    }
}


#Preview {
    Work()
}
